package com.mine.distinctionteam.ui.good.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.mine.distinctionteam.R;


import java.util.List;

;

/**
 *
 */
public class GoodsViewPagerAdapter extends PagerAdapter {

    private Context context = null;
    private List<String> imageUrl = null;
    private LayoutInflater inflater = null;
    private OnItemOnClickListener listener;
    private int mSize;

    public GoodsViewPagerAdapter(Context context , List<String> imageUrl) {
        this.context = context;
        this.imageUrl = imageUrl;
        this.mSize=imageUrl.size();
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return mSize;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = inflater.inflate(R.layout.item_viewpager_goods , container , false);
        //初始化填充的布局上的UI控件
        ImageView imageView= (ImageView) view.findViewById(R.id.imageview_viewpager_goods);
        Glide.with(context).load(imageUrl.get(position)).into(imageView);
        //往容器中增加View
        container.addView(view);
        //设置单击监听，运行调用人重写的接口
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemOnClickListener();
            }
        });
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
    //接口，由调用人重写
    public interface OnItemOnClickListener{
        public void onItemOnClickListener();
    }
    //方便调用者设置监听
    public void setOnItemOnClickListener(OnItemOnClickListener _listener){
        this.listener=_listener;
    }

    //刷新数据
    public void reloadViewPager(List<String> _list, boolean isClear) {
        if (isClear) {
            imageUrl.clear();
        }
        imageUrl.addAll(_list);
        notifyDataSetChanged();
    }
}
